const Monitor = require('../../utils/monitor.js').Monitor
const util = require('../../utils/util.js')
Page(Monitor.hookApp({
  data: {
    imgUrl: getApp().globalData.imgUrl,
    order_id: 0
  },
  onLoad(options) {
    let {
      need_pay,
      order_id
    } = options
    this.setData({
      need_pay: need_pay || 0,
      order_id: order_id || 0,
      openid: getApp().globalData.loginData.openid
    })
    if (this.data.order_id) {
      this.submit()
    }
  },
  onShow() {

  },
  getVal(e) {
    this.setData({
      need_pay: e.detail.value
    })
  },
  check() {
    let need_pay = this.data.need_pay;
    this.setData({
      need_pay: need_pay >= 20000 ? 20000 : need_pay
    })
  },
  submit() {
    let {
      order_id,
      need_pay,
      openid
    } = this.data
    util.API('spwx/v1.Vipcard/memberRecharge', {
      order_id,
      need_pay,
      openid
    }).then(({
      code,
      data,
      msg
    }) => {
      if (code == 200) {
        let {
          timeStamp,
          nonceStr,
          package,
          signType,
          paySign
        } = data.return
        //吊起微信支付
        wx.requestPayment({
          timeStamp,
          nonceStr,
          package,
          signType,
          paySign,
          success() {
            wx.showToast({
              title: '充值成功',
              icon: 'none'
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          },
          fail(res) {
            wx.showToast({
              title: '支付失败',
              icon: 'none'
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          }
        })

      } else {
        wx.showToast({
          title: msg,
          icon: 'none'
        })
      }
    })
  }
}))